---
id: 65824111a09164518320088d
title: Step 21
challengeType: 20
dashedName: step-21
---

# --description--

The `filter()` function allows you to select items from an iterable, such as a list, based on the output of a function: 

```py
filter(my_function, my_list)
```

`filter()` returns an iterator in which the elements of `my_list` are included, for which `my_function` returns `True`. An iterator is a special object that enables you to iterate over the elements of a collection, like a list.

Within the `filter_expenses_by_category` function, call `filter()` passing the `lambda` function you wrote in the previous step as the first argument and the `expenses` list as the second argument.

# --hints--

You should have `filter(lambda expense: expense['category'] == category, expenses)` in your `filter_expenses_by_category` function.

```js
({ test: () =>
  {
    const transformedCode = code.replace(/\r/g, "");        
    const foo = __helpers.python.getDef("\n"+transformedCode, "filter_expenses_by_category");
    const {function_body} = foo;    
    assert(function_body.match(/^\s+filter\s*\(\s*lambda\s+expense\s*:\s*expense\s*\[\s*("|')category\1\s*\]\s*==\s*category\s*,\s*expenses\s*\)/m));
  }
})
```

# --seed--

## --seed-contents--

```py
def add_expense(expenses, amount, category):
    expenses.append({'amount': amount, 'category': category})
    
def print_expenses(expenses):
    for expense in expenses:
        print(f'Amount: {expense["amount"]}, Category: {expense["category"]}')
    
def total_expenses(expenses):
    return sum(map(lambda expense: expense['amount'], expenses))
    
--fcc-editable-region--
def filter_expenses_by_category(expenses, category):
    lambda expense: expense['category'] == category
--fcc-editable-region--

expenses = []
```
